<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Route;
use App\Repositories\BLL\WechatUserBLL;
use EasyWeChat\Foundation\Application;

class WechatUserController extends Controller
{
	private $userBLL,$UserService;
	private $currentRouteName;
	
	public function __construct(WechatUserBLL $user,Application $wechat){
		$this->currentRouteName = Route::currentRouteName();
		$this->userBLL = $user;
		$this->UserService = $wechat->user;
	}	
	
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	
    public function index()
    {
        $addButton = sp_get_AddActionButton($this->currentRouteName,"更新用户","updateUser");
        return view('wechat.user.index',
        					["addButton"=>$addButton]);
    }
	public function ajaxIndex()
    {
        $result = $this->userBLL->ajaxIndex($this->currentRouteName);
        return response()->json($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
	
	//更新用户列表
	public function updateUser(){
		$openid = null;
		$count = 0;
		do{
			//获取用户列表
			$list = $this->UserService->lists($openid);
			$count = $list['count'];
			$openid = $list['next_openid'];
			if($count>0){
				//获取详情
				$users = $this->UserService->batchGet($list['data']['openid']);
				if(!$this->userBLL->updateAllUsers($users['user_info_list'])){
					flash('更新失败','error');
					break;
				}
			}
		}while($count>0);
		flash('更新成功');
		return redirect(route('wechatuser.index'));
	}
	
}
